﻿using AutoMapper;
using Domain.Process_Procedures;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.ProcessCategory;
using ReturnValue;

namespace MES.Server.API.Application.Handler.ProcessCategory
{
    public class UpdateProcessCategoryHandler : IRequestHandler<UpdateProcessCategoryCommand, ApiResult<int>>
    {
        private readonly IRepository<ProcessCategoryModel> categoryRepo;
        private readonly IMapper mapper;

        public UpdateProcessCategoryHandler(IRepository<ProcessCategoryModel> CategoryRepo, IMapper mapper)
        {
            categoryRepo = CategoryRepo;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(UpdateProcessCategoryCommand request, CancellationToken cancellationToken)
        {
            var result = await categoryRepo.GetValue(request.Id);
            if (result == null)
            {
                return new ApiResult<int>().Fail("数据不存在");
            }
            result = mapper.Map(request, result);
            var r= await categoryRepo.UpdateAsync(result);
            if (r > 0)
            {
                return new ApiResult<int>().Success("更新成功", r);
            }
            else
            {
                return new ApiResult<int>().Fail("更新失败");
            }
        }
    }
}
